package com.qianheng.kit.base.util;

import cn.hutool.core.util.StrUtil;
import com.qianheng.kit.base.dto.BizContext;
import com.qianheng.kit.base.enums.ScenceEnum;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
import java.util.Optional;

public class TL {
    private static final ThreadLocal<BizContext> bizHolder = new ThreadLocal<>();

    public static void start() {
        bizHolder.set(new BizContext());
    }

    public static void clear() {
        bizHolder.remove();
    }

    // 追加执行日志
    public static void info(CharSequence template, Object... params) {
        if (bizHolder.get() == null) {
            return;
        }
        bizHolder.get().getLogInfo().appendLog(StrUtil.format(template, params));
    }

    public static String getLogInfo() {
        if (bizHolder.get() == null) {
            return null;
        }
        return bizHolder.get().getLogInfo().getLogsb().toString();
    }

    public static String getBarcode() {
        if (bizHolder.get() == null) {
            return null;
        }
        return bizHolder.get().getBarcode();
    }

    public static @Nonnull ScenceEnum getScence() {
        return Optional.ofNullable(bizHolder.get()).map(BizContext::getScence).orElse(ScenceEnum.UNKNOWN);
    }

    // 追加执行日志
    public static void warn(CharSequence template, Object... params) {
        if (bizHolder.get() == null) {
            return;
        }
        info(template, params);
        bizHolder.get().getLogWarn().appendLog(StrUtil.format(template, params));
    }

    public static String getLogWarn() {
        if (bizHolder.get() == null) {
            return null;
        }
        return bizHolder.get().getLogWarn().getLogsb().toString();
    }

    // 追加执行日志，可能有堆栈所以不能追加到info
    public static void error(CharSequence template, Object... params) {
        if (bizHolder.get() == null) {
            return;
        }
        bizHolder.get().getLogError().appendLog(StrUtil.format(template, params));
    }

    public static String getLogError() {
        if (bizHolder.get() == null) {
            return null;
        }
        return bizHolder.get().getLogError().getLogsb().toString();
    }

    //todo qh 应该实现日志拦截器，动态输出debug日志，业务侵入小，且能打印框架日志。
    @Deprecated
    public static void trace(Runnable traceLog) {
        if (bizHolder.get() == null) {
            return;
        }
        if (bizHolder.get().isEnableTrace()) {
            try {
                traceLog.run();
            } catch (Exception ignored) {
            }
        }
    }

    public static void setBarcodeIfNull(String barcode) {
        if (bizHolder.get() == null) {
            return;
        }
        if (bizHolder.get().getBarcode() == null) {
            bizHolder.get().setBarcode(barcode);
        }
    }

    public static void setScenceIfNull(ScenceEnum scence) {
        if (bizHolder.get() == null) {
            return;
        }
        if (bizHolder.get().getScence() == null) {
            bizHolder.get().setScence(scence);
        }
    }

    public static void setForeignIdIfNull(Long foreignId, String tb) {
        if (bizHolder.get() == null) {
            return;
        }
        if (bizHolder.get().getForeignId() == null) {
            bizHolder.get().setForeignId(foreignId).setForeignTb(tb);
        }
    }

    public static Optional<BizContext> optBizContext() {
        return Optional.ofNullable(bizHolder.get());
    }

    public static void addSubBizContextIfPresent(BizContext bc) {
        if (bizHolder.get() == null) {
            return;
        }
        if (bc == null) {
            return;
        }
        bizHolder.get().addSubBizContext(bc);
    }

    public static String printAll() {
        if (bizHolder.get() == null) {
            return null;
        }
        return bizHolder.get().printRecursivelyAll();
    }

}
